﻿using ERP.RBAC.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;

namespace ERP.RBAC
{
    public class LoginServices : ILoginServices
    {
        private readonly IRepository<Users> _userrepository;

        public LoginServices(IRepository<Users> userrepository)
        {
            _userrepository = userrepository;
        }

        public async Task<List<UserDTO>> Login(string userName, string Password)
        {
            // 查询用户是否存在
            var user = await _userrepository.FirstOrDefaultAsync(u => u.Username == userName);
            
            if (user == null)
            {
                throw new Exception("用户不存在");
            }

            // 验证密码
            if (user.Password != Password)
            {
                throw new Exception("密码错误");
            }

            // 登录成功，返回用户信息
            return new List<UserDTO>
            {
                new UserDTO
                {
                    Id = user.Id,
                    Username = user.Username,
                    Password = user.Password,
                    UserNickname = user.UserNickname
                }
            };
        }
    }
}
